package com.zzh.custom.dialog;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;


import com.zzh.custom.R;

import java.util.List;

/**
 * @author zhangzhihao
 * @email zhangzhihao@gongjiangren.net
 * @date 2020-11-20
 * @desc 公用的弹窗
 */
public class CommonDialog extends DialogFragment {


    public interface OnLeftClickListener {
        void onClick(CommonDialog commonDialog);
    }

    public interface OnRightClickListener {
        void onClick(CommonDialog commonDialog);
    }

    public interface OnItemClickListener<B extends ItemBean> {
        void onClick(int postion, B bean);
    }

    private AppCompatActivity mActivity;
    private Fragment mFragment;
    /**
     * 点击事件
     */
    private OnLeftClickListener mOnLeftClickListener;
    private OnRightClickListener mOnRightClickListener;
    private OnItemClickListener mOnItemClickListener;


    /**
     * 展示的title
     */
    private String title;
    /**
     * 展示的内容
     */
    private String content;
    /**
     * 左部按钮文案
     */
    private String left;
    /**
     * 右部按钮文案
     */
    private String right;
    /**
     * 展示列表数据
     */
    private List<? extends ItemBean> contents;
    /**
     * 显示的layoutid
     */
    private int layoutId;
    /**
     * 显示item的id
     */
    private RecyclerView.Adapter mAdapter;

    /**
     * 文案的大小
     */
    private int titleSize;
    private int contentSize;
    private int leftSize;
    private int rightSize;
    /**
     * 尺寸大小设置
     */
    private int width;
    private int height;
    private float widthRatio;
    private float heightRatio;

    /**
     * 显示状态
     */
    private boolean isShowTitle;
    private boolean isShowContent;
    private boolean isShowList;
    private boolean isShowLeft;
    private boolean isShowRight;
    private boolean isShowHorizontalLine;
    private boolean isShowVerticalLine;


    /**
     * 控件部分
     */
    private TextView mTvTitle;
    private TextView mTvContent;
    private TextView mTvLeft;
    private TextView mTvRight;
    private View mHorizontalLine;
    private View mVerticalLine;
    private RecyclerView mRContents;
    private View mLeftAndRight;

    private CommonDialog(AppCompatActivity activity) {
        this.mActivity = activity;
    }

    private CommonDialog(Fragment fragment) {
        this.mFragment = fragment;
    }

    private View mRootView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        if (mRootView == null) {
            mRootView = inflater.inflate(layoutId > 0 ? layoutId : R.layout.tool_dialog_common, null, false);
            initView(mRootView);
            setClick();
        }
        config();
        return mRootView;
    }


    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        Dialog dialog = getDialog();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            dialog.getWindow().setLayout(calculateSize(width, widthRatio, dm.widthPixels),
                    calculateSize(height, heightRatio, dm.heightPixels));
        }
    }

    private int calculateSize(int size, float ratio, int pixels) {
        if (size > 0) {
            return size > pixels ? pixels : size;
        }
        if (ratio > 0) {
            int end = (int) (pixels * ratio);
            return end > pixels ? pixels : end;
        }
        return WindowManager.LayoutParams.WRAP_CONTENT;
    }

    /**
     * 初始化控件
     *
     * @param view
     */
    private void initView(View view) {
        mTvTitle = view.findViewById(R.id.title);
        mTvContent = view.findViewById(R.id.content);
        mTvLeft = view.findViewById(R.id.left);
        mTvRight = view.findViewById(R.id.right);
        mHorizontalLine = view.findViewById(R.id.horizontal_line);
        mVerticalLine = view.findViewById(R.id.vertical_line);
        mRContents = view.findViewById(R.id.contents);
        mLeftAndRight = view.findViewById(R.id.leftAndRight);
    }

    private void setClick() {
        mTvLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnLeftClickListener != null) {
                    mOnLeftClickListener.onClick(CommonDialog.this);
                }
                dismiss();
            }
        });
        mTvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnRightClickListener != null) {
                    mOnRightClickListener.onClick(CommonDialog.this);
                }
                dismiss();
            }
        });
    }


    /**
     * 配置显示数据
     */
    private void config() {
        // title
        if (isShowTitle) {
            mTvTitle.setVisibility(View.VISIBLE);
            mTvTitle.setText(TextUtils.isEmpty(title) ? "title" : title);
            mTvTitle.setTextSize(titleSize);
        } else {
            mTvTitle.setVisibility(View.GONE);
        }
        // 分割线
        mHorizontalLine.setVisibility(isShowHorizontalLine ? View.VISIBLE : View.GONE);
        mVerticalLine.setVisibility(isShowVerticalLine ? View.VISIBLE : View.GONE);
        // 操作按钮
        mLeftAndRight.setVisibility((isShowLeft || isShowRight) ? View.VISIBLE : View.GONE);
        mHorizontalLine.setVisibility((isShowLeft || isShowRight) ? View.VISIBLE : View.GONE);
        if (isShowLeft) {
            mTvLeft.setVisibility(View.VISIBLE);
            mTvLeft.setText(TextUtils.isEmpty(left) ? "取消" : left);
            mTvLeft.setTextSize(leftSize);
        } else {
            mTvLeft.setVisibility(View.GONE);
            mVerticalLine.setVisibility(View.GONE);
        }
        if (isShowRight) {
            mTvRight.setVisibility(View.VISIBLE);
            mTvRight.setText(TextUtils.isEmpty(right) ? "确认" : right);
            mTvRight.setTextSize(rightSize);
        } else {
            mTvRight.setVisibility(View.GONE);
            mVerticalLine.setVisibility(View.GONE);
        }
        // 内容区操作
        if (isShowList) {
            mRContents.setVisibility(View.VISIBLE);
            mTvContent.setVisibility(View.GONE);
            if (contents == null) {
                mRContents.setVisibility(View.GONE);
            } else {
                if (mAdapter == null) {
                    mAdapter = new MyAdapter(contents);
                }
                mRContents.setAdapter(mAdapter);
            }
        } else {
            mRContents.setVisibility(View.GONE);
            if (isShowContent) {
                mTvContent.setVisibility(View.VISIBLE);
                mTvContent.setText(TextUtils.isEmpty(content) ? "content" : content);
                mTvContent.setTextSize(contentSize);
            } else {
                mTvContent.setVisibility(View.GONE);
            }
        }
    }

    public void show(@NonNull String dialogName) {
        show(mActivity == null ? mFragment.getChildFragmentManager() : mActivity.getSupportFragmentManager(), dialogName);
    }

    public static class Builder {
        private String title;
        private String content;
        private String left = "取消";
        private String right = "确认";
        private List<? extends ItemBean> contents;
        private int titleSize = 18;
        private int contentSize = 14;
        private int leftSize = 14;
        private int rightSize = 14;
        private int width = -1;
        private int height = -1;
        private float widthRatio = 0.75f;
        private float heightRatio = -1;

        private boolean isShowTitle = true;
        private boolean isShowLeft = true;
        private boolean isShowRight = true;
        private boolean isShowHorizontalLine = true;
        private boolean isShowVerticalLine = true;
        private boolean isShowList = false;
        private boolean isShowContent = true;
        private int layoutId = -1;
        private RecyclerView.Adapter mAdapter;

        private OnLeftClickListener mOnLeftClickListener;
        private OnRightClickListener mOnRightClickListener;
        private OnItemClickListener mOnItemClickListener;

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setContent(String content) {
            this.content = content;
            return this;
        }

        public Builder setContents(List<? extends ItemBean> contents) {
            this.contents = contents;
            return this;
        }

        public Builder setContentSize(int contentSize) {
            this.contentSize = contentSize;
            return this;
        }

        public Builder setLeft(String left) {
            this.left = left;
            return this;
        }

        public Builder setLeftSize(int leftSize) {
            this.leftSize = leftSize;
            return this;
        }

        public Builder setRight(String right) {
            this.right = right;
            return this;
        }

        public Builder setRightSize(int rightSize) {
            this.rightSize = rightSize;
            return this;
        }

        public Builder setShowHorizontalLine(boolean showHorizontalLine) {
            isShowHorizontalLine = showHorizontalLine;
            return this;
        }

        public Builder setShowLeft(boolean showLeft) {
            isShowLeft = showLeft;
            return this;
        }

        public Builder setShowRight(boolean showRight) {
            isShowRight = showRight;
            return this;
        }

        public Builder setShowTitle(boolean showTitle) {
            isShowTitle = showTitle;
            return this;
        }

        public Builder setShowVerticalLine(boolean showVerticalLine) {
            isShowVerticalLine = showVerticalLine;
            return this;
        }

        public Builder setTitleSize(int titleSize) {
            this.titleSize = titleSize;
            return this;
        }

        public Builder setLayoutId(int layoutId) {
            this.layoutId = layoutId;
            return this;
        }

        public Builder setAdapter(RecyclerView.Adapter adapter) {
            mAdapter = adapter;
            return this;
        }

        public Builder setShowList(boolean showList) {
            isShowList = showList;
            return this;
        }

        public Builder setShowContent(boolean showContent) {
            isShowContent = showContent;
            return this;
        }

        public Builder setOnItemClickListener(OnItemClickListener onItemClickListener) {
            mOnItemClickListener = onItemClickListener;
            return this;
        }

        public Builder setOnLeftClickListener(OnLeftClickListener onLeftClickListener) {
            mOnLeftClickListener = onLeftClickListener;
            return this;
        }

        public Builder setOnRightClickListener(OnRightClickListener onRightClickListener) {
            mOnRightClickListener = onRightClickListener;
            return this;
        }

        public Builder setHeight(int height) {
            this.height = height;
            return this;
        }

        public Builder setHeightRatio(float heightRatio) {
            this.heightRatio = heightRatio;
            return this;
        }

        public Builder setWidth(int width) {
            this.width = width;
            return this;
        }

        public Builder setWidthRatio(float widthRatio) {
            this.widthRatio = widthRatio;
            return this;
        }

        private CommonDialog create(AppCompatActivity appCompatActivity, Fragment fragment) {
            CommonDialog commonDialog = appCompatActivity == null ? new CommonDialog(fragment) : new CommonDialog(appCompatActivity);
            commonDialog.content = content;
            commonDialog.title = title;
            commonDialog.contents = contents;
            commonDialog.left = left;
            commonDialog.right = right;
            commonDialog.titleSize = titleSize;
            commonDialog.contentSize = contentSize;
            commonDialog.leftSize = leftSize;
            commonDialog.rightSize = rightSize;
            commonDialog.width = width;
            commonDialog.height = height;
            commonDialog.heightRatio = heightRatio;
            commonDialog.widthRatio = widthRatio;
            commonDialog.isShowHorizontalLine = isShowHorizontalLine;
            commonDialog.isShowLeft = isShowLeft;
            commonDialog.isShowRight = isShowRight;
            commonDialog.isShowList = isShowList;
            commonDialog.isShowContent = isShowContent;
            commonDialog.isShowTitle = isShowTitle;
            commonDialog.isShowVerticalLine = isShowVerticalLine;
            commonDialog.layoutId = layoutId;
            commonDialog.mAdapter = mAdapter;
            commonDialog.mOnItemClickListener = mOnItemClickListener;
            commonDialog.mOnLeftClickListener = mOnLeftClickListener;
            commonDialog.mOnRightClickListener = mOnRightClickListener;
            return commonDialog;
        }

        public CommonDialog create(AppCompatActivity appCompatActivity) {
            return create(appCompatActivity, null);
        }

        public CommonDialog create(Fragment fragment) {
            return create(null, fragment);
        }
    }

    class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

        List<? extends ItemBean> mItemBeans;

        public MyAdapter(List<? extends ItemBean> list) {
            this.mItemBeans = list;
        }

        @NonNull
        @Override

        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            ViewHolder viewHolder = new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.tool_item_dialog_common, null, false));
            viewHolder.name.setOnClickListener(mToolOnClickListener);
            return viewHolder;
        }

        @Override
        public int getItemCount() {
            return mItemBeans.size();
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            holder.name.setText(mItemBeans.get(position).getCommonItemName());
            holder.line.setVisibility(position == getItemCount() - 1 ? View.GONE : View.VISIBLE);
            holder.name.setTag(position);
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            TextView name;
            View line;

            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                name = itemView.findViewById(R.id.itemName);
                line = itemView.findViewById(R.id.line);
            }
        }

        private View.OnClickListener mToolOnClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = (int) v.getTag();
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onClick(position, mItemBeans.get(position));
                }
                dismiss();
            }
        };
    }

    public interface ItemBean {
        String getCommonItemName();
    }

}
